﻿hpxAdminApp.factory('customerService', ['Restangular', function (Restangular) {
    var res = Restangular.all('customers');
    return {
        get: function (id) {
            return res.one(id).get();
        },
        queryAll: function () {
            return res.get('enterpriseAccountCheck', {});
        },
        query: function (params, filter) {
            var queryParam = {
                'p': params.page(),
                'n': params.count(),
                'orderBy': params.orderBy(),
                'isChecked': filter.checkedType,
                'enterpriseName':filter.enterpriseName
            }
            return res.get('enterpriseAccountCheck', queryParam).then(function (result) {
                params.total(result.page_info.items_number);
                return result.accounts;
            });
        },

        getUnverifiedAccount:function(id){
            return res.all('enterpriseAccountCheck').one(id.toString()).get();
        },
        checkAccount:function(id, model){
            return res.all('enterpriseAccountCheck').one(id.toString()).customPUT(model);
        },
        ReviewEnterPriseAccount: function (id, data) {
            return res.one('enterpriseAccountCheck').customPUT(data, id, { 'func': 'auto' });
        },

        queryAllCustomersUnverified: function (params, isChecked) {
            var queryParam = {
                'p': params.page(),
                'n': params.count(),
                'orderBy': params.orderBy(),
                'isChecked': isChecked,
            }
            return res.get('customerReview', queryParam).then(function (result) {
                params.total(result.page_info.items_number);
                return result.enterprises;
            });
        },
        getCustomerUnverified: function (id) {
            return res.all('customerReview').one(id.toString()).get();
        },
        checkCustomerReview: function (id, model) {
            return res.all('customerReview').one(id.toString()).customPUT(model);
        },
        queryAllCustomer: function (params, filter) {
            var queryParam = {
                'p': params.page(),
                'n': params.count(),
                'orderBy': params.orderBy(),
                'enterprise_name': filter.enterprise_name,
                'phone_number': filter.phone_number,
                'isVerified': filter.isVerified
            }
            return res.get('customer', queryParam).then(function (result) {
                params.total(result.page_info.items_number);
                return result.customer_info;
            });
        },
        updateCustomer: function (model) {
            return res.one('customer').customPUT(model);
        },
        remove: function (id) {
            return res.all('customer').customDELETE(id);
        },
        customerReview: function (enterpriseId, model) {
            return res.all('customerReview').one(enterpriseId.toString()).customPOST(model);
        },
        setCustomerLevel: function (customerId, model) {
            return res.all('customer/setLevel').one(customerId.toString()).customPUT(model);
        },
        getEnterpriseMember: function (is_user, customer_id) {
            var queryParam = {
                'is_user': is_user,
                'customer_id': customer_id
            }
            return res.get('enterpriseMember', queryParam);
        }


    }
}]);